#ifndef _DICOM_PUT_HANDLER_H_
#define _DICOM_PUT_HANDLER_H_

#include <map>
#include <onion/put_handler.h>
#include "dicom/storage/local_cache.h"
#include "dicom/storage/stream.h"
#include "dicom/storage/encrypt_stream.h"
#include <gwbase/base/mutex.h>

namespace dicom {

struct PutSink {
  PutSink() : file(NULL), enc(NULL) {}
  ~PutSink() {
    delete file;
    delete enc;
  }
  std::string name;
  FileWriter* file;
  EncryptStream* enc;
  size_t src_size;
};

class PutSinkPool {
 public:
  PutSinkPool(LRUFileCache* fcache) : fcache_(fcache) {}
  ~PutSinkPool() {}

  PutSink* NewPutSink(size_t size);
  void FreePutSink(PutSink* s);

  onion_put_sink_pool_t* OnionPool();

  PutSink* FindSink(const std::string& path);

 private:
  Mutex mu_;
  LRUFileCache* fcache_;
  std::map<std::string, PutSink*> sinks_;
  onion_put_sink_pool_t onion_pool_;
};
}

#endif  // _DICOM_PUT_HANDLER_H_
